import time
from pages.excel_data_extraction import Excel_data_extraction
from config.settings import get_config
from base.base_page import BasePage

"""
封装功能函数：时间同步模块

"""
class Time_Sync(BasePage):
    def __init__(self, driver, device_type, logger=None):
        super().__init__(driver, device_type, logger=logger)  # 传递日志实例
        self.driver = driver
        self.config = get_config(device_type)  # 获取全局配置
        self.excel_processor = Excel_data_extraction(self.driver, device_type, logger=logger)

    ########################################  新建数据接口令牌控制  ########################################

    def enter_time_sync_page(self):
        # 进入时间同步界面
        if self.quick_check_element('time_sync_module', 'time_sync_up_down'):
            pass
        else:
            self.click('first_level_menu_module', 'time_syn')
            self.soft_assert_element_present('time_sync_module', 'time_sync_up_down')

    def time_sync_up(self):
        # 开启时间同步
        self.enter_time_sync_page()
        self.click('time_sync_module', 'time_sync_up')

    def time_sync_down(self):
        # 停用时间同步
        self.enter_time_sync_page()
        self.click('time_sync_module', 'time_sync_down')

    def input_time_server_ip(self, time_server_ip):
        # 输入时间服务器IP地址
        self.enter_time_sync_page()
        self.input_text('time_sync_module', 'input_time_server_ip', time_server_ip)
        self.soft_assert_input_value('time_sync_module', 'input_time_server_ip', time_server_ip)

    def input_sync_time(self, sync_time):
        """ 输入同步时间 """
        self.enter_time_sync_page()
        self.click('time_sync_module', 'input_sync_time')
        selected_result = self.select_options(
            element_locator='input_sync_time_all',
            values=sync_time,
            param_name='sync_time'
        )
        self.soft_assert_text_equal('time_sync_module', 'input_sync_time', sync_time)
        return selected_result

    def save_sync_time(self):
        # 保存同步时间
        self.enter_time_sync_page()
        self.click('time_sync_module', 'save_sync_time')
        self.find_element('common_elements', 'update_success_time_page')

    def select_options(self, element_locator: str, values: list, param_name: str):
        """通用选项选择方法（基础方法）"""
        # 类型统一处理
        if not isinstance(values, list):
            values = [values]

        selected = []
        for value in values:
            # 获取所有可选项
            options = self.find_elements('time_sync_module', element_locator)

            # 标准化输入值
            target_text = str(value).strip()

            # 遍历匹配选项
            found = False
            for option in options:
                option_text = option.text.strip()
                if option_text == target_text:
                    self.highlight_element(option)  # 可视化点击元素
                    option.click()
                    selected.append(option_text)
                    found = True
                    break

            if not found:
                available_options = [opt.text.strip() for opt in options]
                raise ValueError(
                    f"参数 {param_name} 未找到选项: '{target_text}'\n"
                    f"可用选项: {available_options}"
                )
            return selected